package com.hhit.collaborative_learning.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.hhit.collaborative_learning.dao.BBS_TopicStateDao;
import com.hhit.collaborative_learning.entity.BBSTopicState;

public class BBS_TopicStateDaoImpl implements BBS_TopicStateDao {

	@Override
	public int delet(Connection con, String findby, String ids) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM `collaborative_learning`.`bbs_topicstate` WHERE "+findby+" in ("+ids+")";
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			return prs.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			System.out.println("非法sql语句  "+sql);
			return 0;
		} 
	}

	@Override
	public int insert(Connection con,List<BBSTopicState> list) {
		// TODO Auto-generated method stub
		String sql="INSERT INTO `collaborative_learning`.`bbs_topicstate`(`TopicID`,`StateTypeID`,`CreatedOn`)VALUES(?,?,?)";
		try {
			con.setAutoCommit(false);
			PreparedStatement prest =con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			for(BBSTopicState bts:list){
				 prest.setInt(1, bts.getTopicID());
				 prest.setInt(2, bts.getStateTypeID());
				 prest.setTimestamp(3, bts.getCreateOn());
				 prest.addBatch();
			}
			int re[]=prest.executeBatch();
			con.commit();   
			con.setAutoCommit(true);
			int sucNum=0;
			for(int i:re){
				sucNum+=i;
			}
			return sucNum;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("非法sql语句  "+sql);
			return 0;
		}
	}

}
